
UIItem = {
basic = function (obj,v)
	if v.x and v.y then obj:SetPos(v.x,v.y) end
	if v.width and v.height then
		obj:SetSize(v.width,v.height)
		obj.width = v.width
		obj.height = v.height
	end
	if v.name then obj:SetText(v.name) end
	if v.group then
		obj:SetGroup(v.group)
	end

	--For btns in Pop Wins
	if v.pop then
		obj.pop = v.pop
	end
	if v.onclick then
		obj:SetOnClick(v.onclick)
		obj.onclick = v.onclick
	end
end,
static = function (v)
	local s = cDXUIBase:New()
	UIItem.basic(s,v)
	s:SetColor(0,0,0)
	s:SetAlpha(0)
	return s
end,
pushbutton = function (v)
	local btn = cDXUIButton:New()
	UIItem.basic(btn,v)
	return btn
end,
checkbox = function (v)
	local check = cDXUICheckBox:New()
	UIItem.basic(check,v)
	return check
end,
listbox = function (v)
	local list = cDXUIListBox:New()
	UIItem.basic(list,v)
	if v.focuseffect~=nil then
		list:SetFocusEffect(v.focuseffect)
	end
	return list
end,
combobox = function (v)
	local combo = cDXUIComboBox:New()
	UIItem.basic(combo,v)
	if v.comboheight then
		combo:SetSize(v.width,v.height)
	end
	return combo
end,
slider = function (v)
	local s = cDXUISlider:New()
	UIItem.basic(s,v)
	return s
end,
editbox = function(v)
	local edit = cDXUIEditBox:New()
	UIItem.basic(edit,v)
	return edit
end,
window = function(v)
	local Win = cDXUIWindow:New()
	if v.name then Win:SetTitle(v.name) end
	if v.x and v.y then Win:SetPos(v.x,v.y) end
	if v.width and v.height then Win:SetSize(v.width,v.height) end
	return Win
end
}

UILayout = {
CheckItem = function (v)
	v.id = v.id or "noid"
	v.name = v.name or ""
	v.x = v.x or 0
	v.y = v.y or 0
	v.width = v.width or 0
	v.height = v.height or 0
	v.paddingx = v.paddingx or 0
	v.paddingy = v.paddingy or 0
end,
CalSize = function (NumPerRow,UITable)
	local TableWidth = 0
	local TableHeight = 0
	local CurRowWidth = 0
	local CurRowHeight = 0
	local CurX = 0
	local CurY = 0
	local Cnt = 1
	for k,v in pairs(UITable.__Container) do

		UILayout.CheckItem(v)

		if v.__Container then
			UILayout.CalSize (NumPerRow,v)
		end

		CurRowWidth = CurRowWidth + v.width
		CurRowHeight = math.max(CurRowHeight,v.height)

		v.x = CurX + v.paddingx/2
		v.y = CurY + v.paddingy/2
		v.width = v.width - v.paddingx
		v.height = v.height - v.paddingy

		CurX = CurX + v.width + v.paddingx
		if v.type == "break" or Cnt%NumPerRow == 0 then
			TableWidth = math.max(TableWidth, CurRowWidth)
			TableHeight = TableHeight + CurRowHeight
			CurX = 0
			CurY = CurY + CurRowHeight
			CurRowWidth = 0
			CurRowHeight = 0
			Cnt = 0
		end
		Cnt = Cnt + 1
	end

	if Cnt ~= 0 then
		TableWidth = math.max(TableWidth, CurRowWidth)
		TableHeight = TableHeight + CurRowHeight
	end

	UITable.width = TableWidth
	UITable.height = TableHeight
end,
CreateItem = function (Parent,UITable,StoreUI)
	for k,v in pairs(UITable.__Container) do
		if v.type and UIItem[v.type] then
			local CurSubUI = UIItem[v.type](v)
			if v.id then
				StoreUI[v.id] = CurSubUI
				table.insert(StoreUI,CurSubUI)
			end
			if v.__Container then
				UILayout.CreateItem(CurSubUI,v,StoreUI)
			end
			Parent:AddDXUI(CurSubUI)
			TRACE(string.format("DXUI - %s id( %s ) size(%d, %d) created.\n",v.type,v.id,v.width,v.height))
		end
	end
end,
Create = function (UITable,Parent)
	Parent = Parent or cWindow
	if not UITable.type or not UIItem[UITable.type] then
		TRACE("UITable no type specified.\n")
		return
	end
	UILayout.CheckItem(UITable)
	local CurUI = UIItem[UITable.type](UITable)
	cWindow:AddDXUI(CurUI)
	TRACE(string.format("DXUI - %s ( %s ) size( %d, %d) created.\n",UITable.type,UITable.name,UITable.width,UITable.height))
	CurUI:SetPos(UITable.x,UITable.y)
	CurUI:SetSize(UITable.width,UITable.height)
	UITable.__SelfUI = CurUI
	UILayout.CreateItem(CurUI,UITable,UITable)
	TRACE("DXUI - CreateUI succeeded.\n")
end,
__CreatePopRecursive = function (UITable,All)
	local MySon = {}
	for k,v in ipairs(UITable.__Container) do
		if v.pop then
			table.insert(MySon,v.pop)
			UILayout.__CreatePopRecursive(v.pop,All)
		end
	end
	UILayout.Create(UITable)

	UITable.Hide = function ()
		UITable.Visible = false
		UITable.__SelfUI:SetVisible(false)
		for k,v in ipairs(MySon) do
			v.Hide()
		end
	end
	UITable.Show = function ()
		for k,v in ipairs(UITable.Bros) do
			v.Hide()
		end
		UITable.Visible = true
		UITable.__SelfUI:SetVisible(true)
	end
	UITable.MoveTo = function (x,y)
		for k,v in ipairs(MySon) do
			v.Hide()
		end
		UITable.__SelfUI:SetPos(x,y)
	end
	for k,v in ipairs(UITable) do
		if v.onclick then
			v:SetOnClick(function()
				v.onclick()
				for _,vv in ipairs(All) do
					vv.Hide()
				end
			end)
		end
		if v.pop then
			v:SetOnClick(function ()
				local x = UITable.__SelfUI:GetX() + v.width
				local y = UITable.__SelfUI:GetY()+v:GetY()
				local bias = cWindow:GetClientSize()
				bias.x = x + v.pop.width - bias.x
				bias.y = y + v.pop.height - bias.y
				if bias.x>0 then
					x = x - bias.x
				end
				if bias.y>0 then
					y = y - bias.y
				end
				v.pop.__SelfUI:SetPos(x,y)
				v.pop.Visible = not v.pop.Visible
				if v.pop.Visible then
					v.pop.Show()
				else v.pop.Hide() end
			end)
		end
	end
end,
__FindAllPop = function(UITable,All)
	table.insert(All,UITable)
	for k,v in ipairs(UITable.__Container) do
		if v.pop then
			UILayout.__FindAllPop(v.pop,All)
		end
	end
end,
__SetPopBros = function (UITable)
	local CurBros = {}
	for k,v in ipairs(UITable.__Container) do
		if v.pop then
			UILayout.__SetPopBros(v.pop)
			table.insert(CurBros,v.pop)
		end
	end
	for k,v in ipairs(UITable.__Container) do
		if v.pop then
			v.pop.Bros = CurBros
		end
	end
end,
CreatePop = function (UITable)
	local AllPop = {}
	UILayout.__FindAllPop(UITable,AllPop)
	UILayout.__SetPopBros(UITable)
	UITable.Bros = {}
	UILayout.__CreatePopRecursive(UITable,AllPop)

	TRACE("DXUI - PopWin Created.\n")
end
}


--[[
for k,v in pairs(CustomLayout) do
	if type(v)=="table" then
		print(k,v.type,"x:"..v.x,"y:"..v.y,"width:"..v.width,"height:"..v.height)
		TRACE(string.format("type:%s x:%d y:%d width:%d height:%d\n",v.type,v.x,v.y,v.width,v.height))
	end
end--]]
